package cn.keepme.ep.common.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jweb.core.bean.ReplyDataMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.keepme.ep.auth.controller.AuthenController;
import cn.keepme.ep.auth.transfer.vo.authen.AuthUserVO;
import cn.keepme.ep.common.service.SysServiceI;
import cn.keepme.ep.util.CommonMethod;

@Controller
@RequestMapping("/sys")
public class SysController {
	@Autowired
	private AuthenController authenController;
	@Autowired
	private SysServiceI sysService;

	@RequestMapping(value = "/heart")
	@ResponseBody
	public ReplyDataMode heart(){
		ReplyDataMode j = new ReplyDataMode();
		j.setSuccess(true);
		
		Subject currentUser = SecurityUtils.getSubject();
		
		if(currentUser.isAuthenticated()){
			AuthUserVO user = (AuthUserVO)currentUser.getSession().getAttribute("userInfo");
//			System.out.println("#### sys/heart user ="+user.getUlist().get(0).toString());
			j.setData(user);
			j.setSuccess(true);
			
		} else {
			j.setSuccess(false);
			j.setData("no");
		}
		
		return j;
	}


	@RequestMapping(value = "/heartTmpUser")
	@ResponseBody
	public ReplyDataMode heartTmpUser(){
		ReplyDataMode j = new ReplyDataMode();
		j.setSuccess(true);
		
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVO userInfoTmp = (AuthUserVO)currentUser.getSession().getAttribute("userInfoTmp");  // 临时用户
		
		if(userInfoTmp!=null){
//			System.out.println("#### sys/heartTmpUser userInfoTmp ="+userInfoTmp.getUlist().get(0).toString());
			j.setData(userInfoTmp);
			j.setSuccess(true);
		} else {
			j.setSuccess(false);
			j.setData("no");
		}
	
		return j;
	}
	

	@RequestMapping(value = "/loginTmp")
	@ResponseBody
	public ReplyDataMode loginTmp(String userId){
		ReplyDataMode j = new ReplyDataMode();
		Subject currentUser = SecurityUtils.getSubject();
		if(userId!=null&&!"".equals(userId)){			
			AuthUserVO user = new AuthUserVO();
			List<cn.keepme.ep.auth.transfer.vo.authen.UserCategoryTradeVO> ulist = CommonMethod.getTmpUserInfo(this.sysService, userId);
			user.setTmpulist(ulist);
			user.setId(ulist.get(0).getTmpUserId());
			user.setTradeName(ulist.get(0).getTradename());
			currentUser.getSession().setAttribute("userInfoTmp", user);
			j.setData(user);
			j.setSuccess(true);
		}else{
			AuthUserVO userInfoTmp = (AuthUserVO)currentUser.getSession().getAttribute("userInfoTmp");  // 临时用户
			if(userInfoTmp!=null){
				j.setData(userInfoTmp);
				j.setSuccess(true);
			} else {
				j.setSuccess(false);
				j.setData("no");
			}
		}
		return j;
	}
	

	/*
	@RequestMapping(value = "/heart")
	@ResponseBody
	public ReplyDataMode heart(){
		ReplyDataMode j = new ReplyDataMode();
		j.setSuccess(true);
		
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVO userInfoTmp = (AuthUserVO)currentUser.getSession().getAttribute("userInfoTmp");  // 临时用户
		
		if(currentUser.isAuthenticated()){
			AuthUserVO user = (AuthUserVO)currentUser.getSession().getAttribute("userInfo");
			System.out.println("#### sys/heart user ="+user.getUlist().get(0).toString());
			j.setData(user);
			j.setSuccess(true);
			
		} else {
			if(userInfoTmp!=null){
//				System.out.println("#### sys/heartTmpUser userInfoTmp ="+userInfoTmp.getUlist().get(0).toString());
				j.setData(userInfoTmp);
				j.setSuccess(true);
			} else {
				j.setSuccess(false);
				j.setData("no");
			}
//			j.setSuccess(false);
//			j.setData("no");
		}
		
		return j;
	}
	*/
	
}
